package com.mxx.pmxt.service.impl;

import com.mxx.pmxt.dao.GoodsModelMapper;
import com.mxx.pmxt.dao.UserGoodsModelMapper;
import com.mxx.pmxt.model.GoodsModel;
import com.mxx.pmxt.model.UserGoodsModel;
import com.mxx.pmxt.model.pojo.AfterCheckByUserModel;
import com.mxx.pmxt.service.GoodsService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;


import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

/**
 * Created by 马潇霄 on 2017/4/18.
 */
@Service
@Transactional(rollbackFor = Exception.class)
public class GoodsServiceImpl implements GoodsService
{

    @Resource
    GoodsModelMapper goodsModelMapper;

    @Resource
    UserGoodsModelMapper userGoodsModelMapper;

    public int createGoods(String owner, String name, BigDecimal price, String detail, String picture, int kindid)
    {
        GoodsModel goodsModel = new GoodsModel();
        goodsModel.setGname(name);
        goodsModel.setGowner(owner);
        goodsModel.setGprice(price);
        goodsModel.setGpicture(picture);
        goodsModel.setGstate(0);
        goodsModel.setGdetail(detail);
        goodsModel.setGkindid(kindid);
        goodsModelMapper.insertSelective(goodsModel);
        return 1;
    }

    public List<GoodsModel> getBeforeCheckGoodsByUserByPage(String uname, int startRow, int pageSize)
    {
        return goodsModelMapper.selectBeforeCheckByPageByUser(startRow, pageSize, uname);
    }

    public int countBeforeGoodsByUser(String uname)
    {
        return goodsModelMapper.countSelectBeforeCheckByUser(uname);
    }

    public List<GoodsModel> getFalseCheckGoodsByUserByPage(String uname, int startRow, int pageSize)
    {
        return goodsModelMapper.selectFalseCheckByPageByUser(startRow,pageSize,uname);
    }

    public int countFalseGoodsByUser(String uname)
    {
        return goodsModelMapper.countSelectFalseCheckByUser(uname);
    }

    public List<AfterCheckByUserModel> getAfterCheckGoodsByUserByPage(String uname, int startRow, int pageSize)
    {
        List<GoodsModel> goodsModels = goodsModelMapper.selectAfterCheckByPageByUser(startRow, pageSize, uname);
        List<AfterCheckByUserModel> afterCheckByUserModels = new ArrayList<AfterCheckByUserModel>();
        for (GoodsModel goodsModel:goodsModels)
        {
            AfterCheckByUserModel afterCheckByUserModel = new AfterCheckByUserModel();
            afterCheckByUserModel.setGid(goodsModel.getGid());
            afterCheckByUserModel.setGpicture(goodsModel.getGpicture());
            afterCheckByUserModel.setGname(goodsModel.getGname());
            afterCheckByUserModel.setGprice(goodsModel.getGprice());
            BigDecimal nowprice = userGoodsModelMapper.selectMaxPriceByGid(goodsModel.getGid());
            afterCheckByUserModel.setNowprice(nowprice);
            afterCheckByUserModel.setGdetail(goodsModel.getGdetail());
            afterCheckByUserModel.setGkindid(goodsModel.getGkindid());
            afterCheckByUserModel.setGstarttime(goodsModel.getGstarttime());
            afterCheckByUserModel.setGendtime(goodsModel.getGendtime());
            afterCheckByUserModels.add(afterCheckByUserModel);
        }

        return afterCheckByUserModels;
    }

    public int countAfterGoodsByUser(String uname)
    {
        return goodsModelMapper.countSelectAfterCheckByUser(uname);
    }

    public List<AfterCheckByUserModel> getEndCheckGoodsByUserByPage(String uname, int startRow, int pageSize)
    {
        List<GoodsModel> goodsModels = goodsModelMapper.selectEndCheckByPageByUser(startRow, pageSize, uname);
        List<AfterCheckByUserModel> afterCheckByUserModels = new ArrayList<AfterCheckByUserModel>();
        for (GoodsModel goodsModel:goodsModels)
        {
            AfterCheckByUserModel afterCheckByUserModel = new AfterCheckByUserModel();
            afterCheckByUserModel.setGid(goodsModel.getGid());
            afterCheckByUserModel.setGpicture(goodsModel.getGpicture());
            afterCheckByUserModel.setGname(goodsModel.getGname());
            afterCheckByUserModel.setGprice(goodsModel.getGprice());
            BigDecimal nowprice = userGoodsModelMapper.selectMaxPriceByGid(goodsModel.getGid());
            afterCheckByUserModel.setNowprice(nowprice);
            afterCheckByUserModel.setGdetail(goodsModel.getGdetail());
            afterCheckByUserModel.setGkindid(goodsModel.getGkindid());
            afterCheckByUserModel.setGstarttime(goodsModel.getGstarttime());
            afterCheckByUserModel.setGendtime(goodsModel.getGendtime());
            afterCheckByUserModels.add(afterCheckByUserModel);
        }

        return afterCheckByUserModels;
    }

    public int countEndGoodsByUser(String uname)
    {
        return goodsModelMapper.countSelectEndCheckByUser(uname);
    }

    public List<GoodsModel> getBeforeCheckGoodsByPage(int startRow, int pageSize)
    {
        return goodsModelMapper.selectBeforCheckByPage(startRow, pageSize);
    }

    public int countBeforeGoods()
    {
        return goodsModelMapper.countSelectBeforeCheck();
    }

    public List<GoodsModel> getAfterCheckGoodsByPage(int startRow, int pageSize)
    {
        return goodsModelMapper.selectAfterCheckByPage(startRow, pageSize);
    }

    public int countAfterGoods()
    {
        return goodsModelMapper.countSelectAfterCheck();
    }

    public List<GoodsModel> getFalseCheckGoodsByPage(int startRow, int pageSize)
    {
        return goodsModelMapper.selectFalseCheckByPage(startRow, pageSize);
    }

    public int countFalseGoods()
    {
        return goodsModelMapper.countSelectFalseCheck();
    }

    public List<GoodsModel> getEndCheckGoodsByPage(int startRow, int pageSize)
    {
        return goodsModelMapper.selectEndCheckByPage(startRow, pageSize);
    }

    public int countEndGoods()
    {
        return goodsModelMapper.countSelectEndCheck();
    }

    public int checkTrueGoods(int gid)
    {

        GoodsModel goodsModel = goodsModelMapper.selectByPrimaryKey(gid);
        goodsModel.setGstarttime(new Date());
        Calendar cal = Calendar.getInstance();
        cal.setTime(new Date());
        cal.add(Calendar.DATE, 2);
        goodsModel.setGendtime(cal.getTime());
        goodsModel.setGstate(1);
        goodsModelMapper.updateByPrimaryKeySelective(goodsModel);

        UserGoodsModel userGoodsModel = new UserGoodsModel();
        userGoodsModel.setPgoodid(gid);
        userGoodsModel.setPisget(0);
        userGoodsModel.setPnowprice(goodsModel.getGprice());
        userGoodsModel.setPnowtime(new Date());
        userGoodsModel.setPuser(goodsModel.getGowner());
        userGoodsModelMapper.insert(userGoodsModel);

        return 1;
    }

    public int checkFalseGoods(int gid)
    {
        GoodsModel goodsModel = goodsModelMapper.selectByPrimaryKey(gid);
        goodsModel.setGstate(3);
        goodsModelMapper.updateByPrimaryKeySelective(goodsModel);
        return 1;
    }

    public List<GoodsModel> getAfterCheckGoodsByPageBykinds(int startRow, int pageSize, int kindid)
    {
        return goodsModelMapper.selectAfterCheckByPageByKinds(startRow, pageSize, kindid);
    }

    public int countAfterGoodsByKinds(int kindid)
    {
        return goodsModelMapper.countSelectAfterCheckByKinds(kindid);
    }

    public GoodsModel selectGoodModelByGid(int gid)
    {
        return goodsModelMapper.selectByPrimaryKey(gid);
    }

    public List<GoodsModel> selectAfterByPageByName(String gname, int startRow, int pageSize)
    {
        return goodsModelMapper.selectAfterByPageByName(gname,startRow,pageSize);
    }

    public int countSelectAfterByName(String gname)
    {
        return goodsModelMapper.countSelectAfterByName(gname);
    }
}
